/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iss.examsys.entity;

import java.io.Serializable;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Quek Ting Hong
 */
@Entity
@Table(name = "question")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Question.findAll", query = "SELECT q FROM Question q"),
    @NamedQuery(name = "Question.findById", query = "SELECT q FROM Question q WHERE q.id = :id"),
    @NamedQuery(name = "Question.findByQuestionVersion", query = "SELECT q FROM Question q WHERE q.questionVersion = :questionVersion"),
    @NamedQuery(name = "Question.findByText", query = "SELECT q FROM Question q WHERE q.text = :text"),
    @NamedQuery(name = "Question.findByMark", query = "SELECT q FROM Question q WHERE q.mark = :mark"),
    @NamedQuery(name = "Question.findByMasterId", query = "SELECT q FROM Question q WHERE q.masterId = :masterId"),
    @NamedQuery(name = "Question.findByDeprecated", query = "SELECT q FROM Question q WHERE q.deprecated = :deprecated"),    
    @NamedQuery(name = "Question.findByCreatedDate", query = "SELECT q FROM Question q WHERE q.createdDate = :createdDate"),
    @NamedQuery(name = "Question.findByCreatedBy", query = "SELECT q FROM Question q WHERE q.createdBy = :createdBy"),
    @NamedQuery(name = "Question.findByModifiedDate", query = "SELECT q FROM Question q WHERE q.modifiedDate = :modifiedDate"),
    @NamedQuery(name = "Question.findByModifiedBy", query = "SELECT q FROM Question q WHERE q.modifiedBy = :modifiedBy")})
public class Question implements Serializable {
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "questionId")
    private List<SectionQuestion> sectionQuestionList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "questionId")
    private List<QuestionTag> questionTagList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "questionId")
    private List<ExamInstanceQuestion> examInstanceQuestionList;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID")
    private Long id;
    @Column(name = "QUESTION_VERSION")
    private Integer questionVersion;
    @Size(max = 1000)
    @Column(name = "TEXT")
    private String text;
    @Basic(optional = false)
    @NotNull
    @Column(name = "MARK")
    private int mark;
    @Column(name = "MASTER_ID")
    private BigInteger masterId;
    @Size(max = 50)
    @Column(name = "DEPRECATED")
    private String deprecated;
    @Version
    private Integer version;
    @Column(name = "CREATED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date createdDate;
    @Column(name = "CREATED_BY")
    private BigInteger createdBy;
    @Column(name = "MODIFIED_DATE")
    @Temporal(TemporalType.TIMESTAMP)
    private Date modifiedDate;
    @Column(name = "MODIFIED_BY")
    private BigInteger modifiedBy;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "questionId")
    private List<QuestionPart> questionPartList;
    @JoinColumn(name = "MODULE_ID", referencedColumnName = "ID")
    @ManyToOne(optional = false)
    private Module moduleId;

    public Question() {
    }

    public Question(Long id) {
        this.id = id;
    }

    public Question(Long id, int mark) {
        this.id = id;
        this.mark = mark;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getQuestionVersion() {
        return questionVersion;
    }

    public void setQuestionVersion(Integer questionVersion) {
        this.questionVersion = questionVersion;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getMark() {
        return mark;
    }

    public void setMark(int mark) {
        this.mark = mark;
    }

    public BigInteger getMasterId() {
        return masterId;
    }

    public void setMasterId(BigInteger masterId) {
        this.masterId = masterId;
    }

    public String getDeprecated() {
        return deprecated;
    }

    public void setDeprecated(String deprecated) {
        this.deprecated = deprecated;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    public BigInteger getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(BigInteger createdBy) {
        this.createdBy = createdBy;
    }

    public Date getModifiedDate() {
        return modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    public BigInteger getModifiedBy() {
        return modifiedBy;
    }

    public void setModifiedBy(BigInteger modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    @XmlTransient
    public List<QuestionPart> getQuestionPartList() {
        return questionPartList;
    }

    public void setQuestionPartList(List<QuestionPart> questionPartList) {
        this.questionPartList = questionPartList;
    }

    public Module getModuleId() {
        return moduleId;
    }

    public void setModuleId(Module moduleId) {
        this.moduleId = moduleId;
    }
    
    public String getQuestionPartsString() {
        StringBuilder sb = new StringBuilder();
        sb.append("<ul>");
        for(QuestionPart qp : getQuestionPartList()) {
            sb.append("<li>").append(qp.getText()).append("</li>");
        }
        return sb.append("</ul>").toString();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Question)) {
            return false;
        }
        Question other = (Question) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.iss.examsys.entity.Question[ id=" + id + " ]";
    }

    @XmlTransient
    public List<SectionQuestion> getSectionQuestionList() {
        return sectionQuestionList;
    }

    public void setSectionQuestionList(List<SectionQuestion> sectionQuestionList) {
        this.sectionQuestionList = sectionQuestionList;
    }

    @XmlTransient
    public List<QuestionTag> getQuestionTagList() {
        return questionTagList;
    }

    public void setQuestionTagList(List<QuestionTag> questionTagList) {
        this.questionTagList = questionTagList;
    }

    @XmlTransient
    public List<ExamInstanceQuestion> getExamInstanceQuestionList() {
        return examInstanceQuestionList;
    }

    public void setExamInstanceQuestionList(List<ExamInstanceQuestion> examInstanceQuestionList) {
        this.examInstanceQuestionList = examInstanceQuestionList;
    }
    
}
